package com.hb.trade.server.project.notice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hb.core.pojo.enums.DeployStatusEnum;
import com.hb.core.pojo.project.notice.mapper.NoticeBidSectionMapper;
import com.hb.core.pojo.project.notice.entity.NoticeBidSection;
import com.hb.core.pojo.project.section.dto.BidSectionDTO;
import com.hb.trade.server.project.notice.service.NoticeBidSectionService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 标段公告中间表 服务实现类
 * </p>
 *
 * @author zhongdl
 * @since 2021-09-10
 */
@Service
public class NoticeBidSectionServiceImpl extends ServiceImpl<NoticeBidSectionMapper, NoticeBidSection> implements NoticeBidSectionService {

    /**
     * 根据标段id 查询最近一次公告
     *
     * @param sectionId
     * @return
     */
    @Override
    public NoticeBidSection findLastNoticeBidSection(Long sectionId) {
        QueryWrapper<NoticeBidSection> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(NoticeBidSection::getSectionId, sectionId).orderByDesc(NoticeBidSection::getCreateTime).last("limit 1");
        return this.getOne(queryWrapper);
    }

    /**
     * 查询该标段关联的公告信息
     *
     * @param sectionId
     * @return
     */
    @Override
    public List<NoticeBidSection> findAllNoticeBidSectionBySectionId(Long sectionId) {
        QueryWrapper<NoticeBidSection> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(NoticeBidSection::getSectionId, sectionId);
        return this.list(queryWrapper);
    }

    @Override
    public List<NoticeBidSection> listNoticeBidSectionByNoticeId(Long noticeId) {
        QueryWrapper<NoticeBidSection> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(NoticeBidSection::getNoticeId, noticeId);
        return this.list(queryWrapper);
    }

    @Override
    public int findDeployedNoticeBySectionId(Long sectionId) {
        return baseMapper.findDeployedNoticeBySectionId(sectionId, DeployStatusEnum.DEPLOYED.getCode());
    }

    /**
     * 查询指定的标段是否已经发布过首次公告
     *
     * @param sectionIds
     * @return
     */
    @Override
    public boolean existsFirstNotice(List<Long> sectionIds) {
        return baseMapper.existsFirstNotice(sectionIds) > 0;
    }

    /**
     * 更新标段, 公告中间表
     */
    @Override
    public void updateNoticeSections(Long noticeId, List<Long> sectionIds) {
        //先删除后加中间表的信息
        QueryWrapper<NoticeBidSection> noticeBidSectionQueryWrapper = new QueryWrapper<>();
        noticeBidSectionQueryWrapper.lambda().eq(NoticeBidSection::getNoticeId, noticeId);
        remove(noticeBidSectionQueryWrapper);

        // 保存中间表信息 (更新标段id公告id)
        List<NoticeBidSection> noticeBidSectionList = new ArrayList<>();
        for (Long sectionId : sectionIds) {
            NoticeBidSection noticeBidSection = new NoticeBidSection();
            noticeBidSection.setNoticeId(noticeId);
            noticeBidSection.setSectionId(sectionId);
            noticeBidSection.buildBaseData(1);
            noticeBidSectionList.add(noticeBidSection);
        }
        saveBatch(noticeBidSectionList);
    }

    @Override
    public List<BidSectionDTO> listNoticeBidSection(Long noticeId) {
        return baseMapper.listNoticeBidSection(noticeId);
    }
}
